#include "cat.h"

Cat::Cat()
{
    image.load("grafx/level2/cat.png");
    music = new MyAudio();
    //catsound.load("sounds/level2/death.wav");

    xpos = 900;
    ypos = 100;
    framex = 0;
    framey = 0;
    sprite_width = 40;
    sprite_height = 50;

    falling = false;
    hit = false;

    counter = 0;
}

int Cat::action(int character_x, int charactery, int scrolling, int damage)
{
    if(scrolling == 1)
       xpos+=5;
    if(scrolling == 0)
       xpos-=5;
    if(xpos-100 <= character_x)
        falling = true;
    if(falling)
    {
        ypos+=5;
    }
    else
        xpos-=3;

    if(ypos > 600)
    {
        xpos = 900;
        ypos = 100;
        falling = false;
    }

    draw();
    return whatHappened(character_x, charactery, damage);

}

int Cat::whatHappened(int character_x, int charactery, int damage)
{
    if(hit)
        if (xpos==900 || ypos>500)
            hit = false;

    if(collision(character_x, charactery, 110, 90, xpos, ypos, 40, 50)=='l' && (damage ==0) )
    {
        //catsound.play();
        xpos = 900;
        ypos = 100;
        falling = false;
        music->create_audio_thread( "sounds/effects/cat.wav", "LO" );


    }

    else if( collision(character_x, charactery, 110, 90, xpos, ypos, 40, 50)=='r' && (damage ==1) )
    {
         //catsound.play();
        xpos = 900;
        ypos = 100;
        falling = false;
        music->create_audio_thread( "sounds/effects/cat.wav", "LO" );

    }
    else if( collision(character_x, charactery, 110, 90, xpos, ypos, 40, 50)!=' ' && !(damage==1 || damage ==0) )
    {
        if(!hit)
        {
            hit = true;
            return 50;
        }

    }


    return 0;


}
void Cat::draw()
{
    animate();
    image.drawAnim( xpos,   ypos,
                    sprite_width, sprite_height,
                    framex, framey);
}

void Cat::animate()
{
    if(counter == 7)
    {
        if(framex < 119)
            framex = framex+40;
        else
            framex =0;
        counter = 0;
    }
    else
       counter++;
}






char Cat::collision(int object1_x, int object1_y, int object1_w, int object1_h,
                       int object2_x, int object2_y, int object2_w, int object2_h)
{
    char result = ' ';

    if (collisionY(object1_y, object1_h, object2_y, object2_h) && ( (object1_x+object1_w) >= (object2_x) && (object1_x+object1_w) <= (object2_x)+100))
    {
        result = 'l';
    }
    else if (collisionY(object1_y, object1_h, object2_y, object2_h) && (  (object1_x)-50 <= (object2_x+object2_w) ))
    {
        result = 'r';
    }
    else if ( collisionX(object1_x, object1_w, object2_x, object2_w) && (  ((object1_y+object1_h) == object2_y) || (  ((object1_y+object1_h) < object2_y+5) && ((object1_y+object1_h) >= object2_y)    )      )   )
    {
        result = 't';
    }
    else if ( collisionX(object1_x, object1_w, object2_x, object2_w) && (  (object1_y == (object2_y+object2_h)) || (   (object1_y > (object2_y+object2_h-5)) && (object1_y <=(object2_y+object2_h))  )      )   )
    {
        result = 'b';
    }
    return result;
}

bool Cat::collisionX(int object1_x, int object1_w,
                       int object2_x, int object2_w)
{
    if( (object1_x+object1_w) >= object2_x && object1_x <= (object2_x+object2_w) )
    {
        return true;
    }
    else
        return false;

}
bool Cat::collisionY(int object1_y, int object1_h,
                       int object2_y, int object2_h)
{
    if( (object1_y+object1_h) >= object2_y && object1_y <= (object2_y+object2_h) )
    {
        return true;
    }
    else
        return false;

}
